<?php

namespace App\V3\V3_2_7\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class MessBoardModel extends Model
{
    use HasFactory;

    protected $table = 'mess_board';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function sellSelGoodsList($data): array
    {
        $list = StorageManageModel::query()
            ->select(
                'b.id',
                'a.id as storage_manage_id',
                'b.materiel_name',
                'b.type_name',
                'b.materiel_img',
                'b.materiel_class_id',
                'b.materiel_unit',
                'b.is_product_materiel',
                'b.is_use_serial',
                'b.cost',
                'b.selling_price',
                'b.remark',
                'b.delete_flag',
                'a.in_storage_num',
                'a.storage_id',
                'e.storage_name',
                'a.shelf_id',
                'a.location_id',
                'c.shelf_name',
                'd.location_name',
            )
            ->from('storage_manage as a')
            ->leftJoin('materiel_type as b', 'b.id', '=', 'a.type_id')
            ->leftJoin('storage_shelf as c', 'c.id', '=', 'shelf_id')
            ->leftJoin('storage_shelf_location as d', 'd.id', '=', 'a.location_id')
            ->leftJoin('storage as e', 'e.id', '=', 'a.storage_id')
            ->where([
                'a.main_id' => $data['main_id'],
                'a.delete_flag' => 0
            ])->where(function ($query) use ($data) {
                if (!empty($data['storage_id'])) {
                    $query->where('a.storage_id', $data['storage_id']);
                }
                if (!empty($data['shelf_id'])) {
                    $query->where('a.shelf_id', $data['shelf_id']);
                }
                if (!empty($data['location_id'])) {
                    $query->where('a.location_id', $data['location_id']);
                }
                if (!empty($data['keyword'])) {
                    $query->where('b.materiel_name', 'like', '%' . $data['keyword'] . '%');
                }
                if (!empty($data['goods_name'])) {
                    $nameArr[] = $data['goods_name'];
                    if (strpos($data['goods_name'], ',') !== false) {
                        $nameArr = explode(',', $data['goods_name']);
                    }
                    $goodsIdArr = [];
                    foreach ($nameArr as $nVal) {
                        $goodsIdArr[] = MaterielTypeModel::query()
                            ->where('materiel_name', 'like', '%' . $nVal . '%')
                            ->pluck('id')->toArray();
                    }
                    $goodsIdArr = collect($goodsIdArr)->flatten()->all();
                    if (!empty($goodsIdArr)) {
                        $query->whereIn('a.type_id', $goodsIdArr);
                    }
                }
            })->paginate($data['pageSize'], ['*'], '', $data['currentPage'])->toArray();
        if (!empty($list['data'])) {
            foreach ($list['data'] as &$lVal) {
                if (!empty($lVal['materiel_img'])) {
                    $materielImg = array_filter(explode(',', $lVal['materiel_img']), function ($v) {
                        return $v !== '';
                    });
                    $lVal['materiel_img'] = $materielImg[0];
                }
            }
        }
        return $list;
    }

    public static function materielSelGoodsList($data): array
    {
        $list = MaterielTypeModel::select(
            'a.id',
            'materiel_name',
            'type_name',
            'materiel_img',
            'materiel_no',
            'materiel_class_id',
            'b.class_name as sec_class_name',
            'c.id as fir_class_id',
            'c.class_name as fir_class_name',
            'a.remark',
            'a.length',
            'a.width',
            'a.height',
            'a.weight',
            'a.attribute',
            'a.cost',
            'a.selling_price',
            'a.is_use_serial',
            'a.materiel_unit',
            'a.lower_selling_price',
            'a.is_product_materiel'
        )
            ->from('materiel_type as a')
            ->leftJoin('materiel_class as b', 'b.id', '=', 'a.materiel_class_id')
            ->leftJoin('materiel_class as c', 'b.parent_id', '=', 'c.id')
            ->where([
                'a.main_id' => $data['main_id'],
                'a.delete_flag' => 0
            ])->where(function ($query) use ($data) {
                if (!empty($data['goods_name'])) {
                    $nameArr[] = $data['goods_name'];
                    if (strpos($data['goods_name'], ',') !== false) {
                        $nameArr = explode(',', $data['goods_name']);
                    }
                    foreach ($nameArr as $nVal) {
                        $query->orWhere('a.materiel_name', 'like', '%' . $nVal . '%');
                    }
                }
                if (!empty($data['keyword'])) {
                    $query->orWhere('b.materiel_name', 'like', '%' . $data['keyword'] . '%');
                    $query->orWhere('b.type_name', 'like', '%' . $data['keyword'] . '%');
                }
            })->orderBy('a.id', 'DESC')
            ->paginate($data['pageSize'], ['*'], '', $data['currentPage'])->toArray();
        if (!empty($list['data'])) {
            foreach ($list['data'] as &$lVal) {
                if (!empty($lVal['materiel_img'])) {
                    $materielImg = array_filter(explode(',', $lVal['materiel_img']), function ($v) {
                        return $v !== '';
                    });
                    $lVal['materiel_img'] = $materielImg[0];
                }
                if ($lVal['is_product_materiel']) {
                    $lVal['content_type_id'] = MaterielProductContentModel::where([
                        'product_id' => $lVal['id']
                    ])->pluck('type_id')->toArray();
                } else {
                    $lVal['content_type_id'] = [];
                }
            }
        }
        return $list;
    }
}
